#version 410 core

// Declare all the semantics
#define ATTR_POSITION	0
#define ATTR_NORMAL		1
#define ATTR_COLOR		2

#define VERT_POSITION	0
#define VERT_NORMAL		1
#define VERT_COLOR		2
#define VERT_INSTANCE	3

#define FRAG_COLOR		0

uniform mat4 View;

layout(location = VERT_NORMAL) in vec3 vNormal;

layout(location = FRAG_COLOR, index = 0) out vec4 fColor;

void main()
{
	vec3 normal = normalize(vNormal);
	vec3 ambiant = vec3(0.2f);
	vec3 lightDir = normalize(vec3(-1.0f));
	vec3 lightDiffuse = vec3(0.6f, 0.6f, 0.9f);
	vec3 lightSpec = vec3(0.2f);
	
	float incidence = -1.0f * dot(normal, lightDir);
	float power = clamp(incidence, 0.0f, 1.0f);
	
	vec3 viewDirection = -(inverse(View)[2].xyz);

	vec3 reflectDir = reflect(viewDirection, normal);
	
	float phongTerm = dot(-lightDir, reflectDir);
	phongTerm = clamp(phongTerm, 0, 1);
	// phongTerm = power != 0.0 ? phongTerm : 0.0;
	phongTerm = pow(phongTerm, 20.0f);

	vec3 diffuse = power * lightDiffuse;
	vec3 specular = phongTerm * lightSpec;
	fColor = vec4(ambiant + diffuse + specular, 1.0f);
}
